﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace AUV5.Web.Service
{
    public partial class RequestPagedList : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = Request["username"] ?? "";
            string category = Request["category"] ?? "";
            int pagesize = 0;
            int pageindex = 0;
            try
            {
                pagesize = int.Parse(Request["pagesize"]??"0");
                pageindex = int.Parse(Request["pageindex"]??"0");
            }
            catch
            {
                Response.Write("113");//无效的整型参数 
                return;
            }
            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey, pageindex, pagesize, username, category))
            {
                Response.Write("112");//密钥不正确
                return;
            }

            if (Request["pageindex"] != null && Request["pagesize"] != null)
            {
                string sqlwhere = string.Format("State={0} and IsDel={1} and StatusID!={2}",(int)Common.Enum.auvState.Enabled,(int)Common.Enum.auvDel.NotDel,(int)Common.Enum.RequestState.Deny);
                //验证类别数据的有效性
                if (!string.IsNullOrEmpty(category))
                {
                    Model.ServiceCategory sc = BLL.ServiceCategoryBLL.Instance.GetCategoryByIDOrCode(category);
                    if (sc != null)
                    {
                        sqlwhere += " and CategoryName='" + sc.CategoryName + "'";
                    }
                    else
                    {
                        Response.Write("302");//类别信息无效
                        return;
                    }
                }
                //用户验证
                if (!string.IsNullOrEmpty(username))
                {
                    Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetailByName(username);
                    if (user != null)
                    {
                        sqlwhere += " and UserID='" + user.UserID + "'";
                    }
                    else
                    {
                        Response.Write("301");//用户编号无效
                        return;
                    }
                }
                
                //只显示可用的,且通过审核的需求
               
                
                int recordcount = 0;
                IList<Model.RequestInfo> list= BLL.RequestInfoBLL.Instance.GetPagedList(pagesize,pageindex,sqlwhere,ref recordcount);

                Hashtable ht = new Hashtable();
                ht.Add("RecordCount",recordcount);
                ht.Add("RequestList", list);

                Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(ht));

            }
            else
            {
                Response.Write("111");//参数不正确
            }

        }

        private bool IsValidKey(string secretkey, int pageindex, int pagesize, string username, string category)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(current_day + (pageindex + pagesize).ToString() + username + category).Substring(6, 22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}